﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
using Alserve.Ffl.Data;
using Alserve.Ffl;
using System.IO;

public partial class ffl_admin_copyplayerimages : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Security.IsAdmin(Page))
            Response.Redirect("~//ffl//anonymous//default.aspx");

    }

    private void DownloadPlayerImages()
    {
        System.Drawing.Image _Image = null;
        EspnPlayer thisEspnPlayer = new EspnPlayer();
        if (thisEspnPlayer.LoadAll())
        {
            while (!thisEspnPlayer.EOF)
            {
                if (!File.Exists(HttpContext.Current.Server.MapPath("~/ffl/images/players/" + thisEspnPlayer.EspnPlayerId.ToString() + ".jpg")))
                {
                    String espnRef = thisEspnPlayer.EspnPlayerId.ToString();
                    _Image = DownloadImage("http://soccernet-assets.espn.go.com/i/players/130x180/" + espnRef + ".jpg");

                    // check for valid image
                    if (_Image != null)
                    {
                        String path = Server.MapPath("../images/players");
                        _Image.Save(path + "\\" + espnRef + ".jpg");
                        _Image.Dispose();
                    }
                }
                thisEspnPlayer.MoveNext();
            }
        }
    }


    private System.Drawing.Image DownloadImage(string _URL)
    {
        System.Drawing.Image _tmpImage = null;

        try
        {
            // Open a connection
            System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_URL);

            _HttpWebRequest.AllowWriteStreamBuffering = true;

            // You can also specify additional header values like the user agent or the referer: (Optional)
            _HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
            _HttpWebRequest.Referer = "http://www.google.com/";

            // set timeout for 20 seconds (Optional)
            _HttpWebRequest.Timeout = 5000;

            // Request response:
            System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse();

            // Open data stream:
            System.IO.Stream _WebStream = _WebResponse.GetResponseStream();

            // convert webstream to image
            _tmpImage = System.Drawing.Image.FromStream(_WebStream);

            // Cleanup
            _WebResponse.Close();
            _WebResponse.Close();
        }
        catch (Exception _Exception)
        {
            // Error
            Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
            return null;
        }

        return _tmpImage;
    }
    protected void DownloadButton_Click(object sender, EventArgs e)
    {
        DownloadPlayerImages();
    }
}
